﻿using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using System;
using DNNToolkit.SQLServerReporting.Components;
using Microsoft.Reporting.WebForms;

namespace DNNToolkit.SQLServerReporting
{
    /// <summary>
    /// The View CrystalReport class displays the content.
    /// </summary>
    public partial class ViewCrystalReport : PortalModuleBase
    {
        private const string SETTINGERRORKEY = "SettingError.Text";

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                ConfigController.CheckToAddConfig(this);

                ShowReport();
            }
        }

        #endregion

        private void ShowReport()
        {
            mReportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            try
            {
                IReportServerCredentials irsc = new CustomReportCredentials(
                    Settings[Contants.KEY_Username].ToString(),
                    Settings[Contants.KEY_Password].ToString(),
                    Settings[Contants.KEY_Domain].ToString());
                mReportViewer.ServerReport.ReportServerCredentials = irsc;
                mReportViewer.ServerReport.ReportServerUrl = new Uri(@Settings[Contants.KEY_ReportServerUrl].ToString());
                mReportViewer.ServerReport.ReportPath = Settings[Contants.KEY_ReportPath].ToString();                
                mReportViewer.ServerReport.Refresh();
                mErrorLable.Visible = false;
            }
            catch 
            {
                // show error message.
                mErrorLable.Text = Localization.GetString(SETTINGERRORKEY, LocalResourceFile);
                mErrorLable.Visible = true;
            }
        }
    }
}